package com.gilight.base.service.impl;

import com.gilight.base.base.ParameterException;
import com.gilight.base.component.CacheComponent;
import com.gilight.base.component.CacheConst;
import com.gilight.base.dao.SysUserZzjgMapper;
import com.gilight.base.dao.SysZzjgMapper;
import com.gilight.base.model.SysZzjg;
import com.gilight.base.service.SysZzjgService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
public class SysZzjgServiceImpl implements SysZzjgService {


    @Resource
    SysZzjgMapper dao;
    @Resource
    SysUserZzjgMapper sysUserZzjgMapper;

    @Autowired
    private CacheComponent cacheComponent;

    @Autowired
    private SysZzjgMapper sysZzjgMapper;


    @Override
    public List<Map<String, Object>> findMapList(Map<String, Object> map) {
        List<Map<String, Object>> list;
        if((map.get("searchName")!=null)&&(!"".equals(String.valueOf(map.get("searchName"))))){
            Map<String, Object> map1=new HashMap<>();
            map1.put("parentId", "-1");
            list=dao.findMapList(map1);//一层
        }else{
            list=dao.findMapList(map);//一层
        }
        if(CollectionUtils.isNotEmpty(list)){
            for(Map<String, Object> xymap:list){
                map.remove("idOr");
                getChildren(map,xymap);
            }
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> findMapListCte(Map<String, Object> map) {
        List<Map<String, Object>> list;
        list = dao.findMapListByCTE(map);
        return list;
    }

    private void getChildren(Map<String, Object> map,Map<String, Object> xymap){
        String parentId=String.valueOf(xymap.get("id"));
        map.put("parentId", parentId);
        List<Map<String, Object>> zyList =dao.findMapList(map);//二层
        if(CollectionUtils.isNotEmpty(zyList)){
            for(Map<String, Object> zymap:zyList){
                this.getChildren(map,zymap);
            }
            xymap.put("children",zyList);
        }
    }
    @Override
    public List<Map<String, Object>> getZzjgType(){
        return dao.getZzjgType();
    }
    @Override
    public List<Map<String, Object>> getZzjgBrand(){
        return dao.getZzjgBrand();
    }
    @Override
    public void addSysZzjg(SysZzjg sysZzjg) {
        checkZzjg(sysZzjg);
        Map<String, Object> map = new HashMap<>();
        map.put("parentId", sysZzjg.getParentId());
        map.put("zzjgName", sysZzjg.getZzjgName());
        int count = dao.countList(map);
        if (count > 0) {
            throw new ParameterException("机构名称不能重复!");
        } else {
            sysZzjg.setStatus(1);
            sysZzjg.setCreateTime(new Date());
            dao.insert(sysZzjg);
        }
    }
    private void checkZzjg(SysZzjg sysZzjg){
        Long parentId = sysZzjg.getParentId();
        if (parentId == null ) {
            throw new ParameterException("机构父id不能为空!");
        }
        String zzjg_type = sysZzjg.getZzjgType();
        if (zzjg_type == null || "".equals(zzjg_type)) {
            throw new ParameterException("组织机构类型不能为空!");
        }
        String deptName = sysZzjg.getZzjgName();
        if (deptName == null || "".equals(deptName)) {
            throw new ParameterException("机构名称不能为空!");
        } else {
            if (deptName.length() > 20) {
                throw new ParameterException("机构名称不能超过20位汉字或字符!");
            }
        }
    }
    @Override
    public void editSysZzjg(SysZzjg sysZzjg) {
      //  checkZzjg(sysZzjg);
        sysZzjg.setUpdateTime(new Date());
        dao.updateByPrimaryKeySelective(sysZzjg);
        cacheComponent.del(CacheConst.ZZJG_CACHE_BY_ID+sysZzjg.getId());
    }
    @Override
    public void delDepartment(Long id){
        if (id == null) {
            throw new ParameterException("机构id不能为空!");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("parentId", id);
        int count = dao.countList(map);
        if (count > 0) {
            throw new ParameterException("此机构含有子机构,无法删除!");
        }
        Map<String, Object> map1 = new HashMap<>();
        map1.put("zzjgId", id);
        int count1 = sysUserZzjgMapper.countList(map1);
        if (count1 > 0) {
            throw new ParameterException("此机构含有用户,无法删除!");
        }
        dao.deleteByPrimaryKey(id);
    }
    @Override
    public SysZzjg getSysZzjgById(Long id){
        if (id == null) {
            throw new ParameterException("机构id不能为空!");
        }
        SysZzjg zzjg = null;
        if(cacheComponent.hasKey(CacheConst.ZZJG_CACHE_BY_ID+id)){
            zzjg = cacheComponent.getObj(CacheConst.ZZJG_CACHE_BY_ID+id,SysZzjg.class);
        }else{
            zzjg = sysZzjgMapper.selectByPrimaryKey(id);
            cacheComponent.putObj(CacheConst.ZZJG_CACHE_BY_ID+id,zzjg,3600);
        }

        return zzjg;
    }

    @Override
    public List<SysZzjg> findByParentId(Long parentId,String type) {
        return dao.findByParentId(parentId, type);
    }
    @Override
    public List<Long> findIdsByParentId(Long parentId){
        /**
         * 先从缓存查
         */
        List list = cacheComponent.getHashObj(CacheConst.ZZJG_CACHE,String.valueOf(parentId),List.class);
        if(list!=null&&list.size()>0){
            return list;
        }
        List<Long> zzjgIds = new ArrayList<>();
        zzjgIds.add(parentId);//先把当前机构加上
        getZzjgIds(parentId,zzjgIds);
        //加入缓存
        cacheComponent.putHashObj(CacheConst.ZZJG_CACHE,String.valueOf(parentId),zzjgIds);
        return zzjgIds;
    }

    public void getZzjgIds(Long parentId,List<Long> zzjgIds){
        List<Long> curentList = dao.findIdsByParentId(parentId);
        if(curentList==null || curentList.size()==0){
            return;
        }else{ //继续查当前机构下的其它子节点
            zzjgIds.addAll(curentList);
            curentList.forEach(item ->{
                this.getZzjgIds(item,zzjgIds);
            });
        }
    }
    public List<Map<String, Object>> getZzjgList() {
        return dao.getZzjgList();
    }

    @Override
    public List<Map<String, Object>> getZzjgData(Map<String, Object> map) {
        return dao.getZzjgData(map);
    }
}
